<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/29
 * Time: 15:16
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Allocation extends Base
{
    /**
     * 政策解读分类展示
     * @return mixed|\think\response\Json
     */
    public function zhengce(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_zhengce_type')
                ->count();// 获取总条数
            $lists = Db::table('up_zhengce_type')
                ->field('zt_id, zt_name')
                ->page($Nowpage,$limits)
//                ->order('zt_update_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 编辑政策解读分类
     * @return mixed|\think\response\Json
     */
    public function zhengceEdit(){
        $zt_id = input('zt_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            
            $update = [
                'zt_name' => $params['zt_name'],
                'zt_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_zhengce_type')->where('zt_id', $zt_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $zhengce = Db::table('up_zhengce_type')
            ->field('zt_name')
            ->where('zt_id', $zt_id)
            ->find();
        $this->assign([
            'zt_id' => $zt_id,
            'zhengce' => $zhengce,
        ]);
        return $this->fetch('allocation/zhengceedit');
    }
    
    /**
     * 联盟资讯分类展示
     * @return mixed|\think\response\Json
     */
    public function information(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_information_type')
                ->count();// 获取总条数
            $lists = Db::table('up_information_type')
                ->field('it_id, it_name')
                ->page($Nowpage,$limits)
//                ->order('it_update_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 编辑联盟资讯分类
     * @return mixed|\think\response\Json
     */
    public function informationEdit(){
        $it_id = input('it_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            
            $update = [
                'it_name' => $params['it_name'],
                'it_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_information_type')->where('it_id', $it_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $information = Db::table('up_information_type')
            ->field('it_name')
            ->where('it_id', $it_id)
            ->find();
        $this->assign([
            'it_id' => $it_id,
            'information' => $information,
        ]);
        return $this->fetch('allocation/informationedit');
    }
}